from pymouse import PyMouse
from pykeyboard import PyKeyboard

m = PyMouse()
k = PyKeyboard()
# x_dim, y_dim = m.screen_size()
# m.click(x_dim/2, y_dim/2, 1, 1)
# k.type_string('Hello, World!')
k.press_keys([k.control_l_key,k.shift_l_key])
# k.press_key(k.control_l_key)
# k.tap_key(k.shift_l_key)
# k.release_key(k.control_l_key)
#k.type_string('hello world')
#k.press_key(k.enter_key)

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Edge()      # Edge浏览器

driver.maximize_window()
# 打开网页
url='http://news.baidu.com/'
driver.get(url)
time.sleep(2)
# //input[@id='kw']

# 使用位置点击 放弃
# kw_x = input_element.location.get('x')
# kw_y = input_element.location.get('y')
# print(input_element.location, ',kw_x = ', kw_x, ',kw_y = ', kw_y)
# m.move(kw_x+10,kw_y+10)
# m.click(kw_x+10,kw_y+10,1)
# 使用webdriver接口激活输入框


## 使用 特定输入法 在浏览器Xpath输入内容
'''
path : Xpath  //input[@id="ww"]
text : 输入内容为 拼音+空格，例如 你好呀=ni hao ya
input_type : 输入法类型 搜狗输入法/百度输入法（依赖提前在输入法中造词 srfmc：搜狗输入法/百度输入法 等）
'''
def switch_input_send_text(path,text,input_type=""):
    for i in range(5):
        input_element = driver.find_element(By.XPATH,path)
        input_element.click()
        time.sleep(1)
        k.type_string('srfmc')  # 依赖提前在输入法中造词 srfmc：搜狗输入法/百度输入法 等，默认为系统自带输入法
        time.sleep(1)
        k.press_key(k.space_key)
        print(input_element.get_attribute('value'))
        if input_element.get_attribute('value') == input_type:
            for i in range(7):
                k.press_keys([k.backspace_key])
            break
        for i in range(7):
            k.press_keys([k.backspace_key])
        k.press_keys([k.control_l_key,k.shift_l_key])

    for t in text.split(" "):
        time.sleep(0.5)
        k.type_string(t)
        time.sleep(0.5)
        k.press_key(k.space_key)

switch_input_send_text(path=r'//input[@id="ww"]',text="ni hao ya",input_type="搜狗输入法")



#import win32api
# import win32gui
# def get_language():
#     """获取当前输入法状态"""
#     hwnd = win32gui.GetForegroundWindow()
#     thread_id = win32api.GetWindowLong(hwnd, 0)
#     print(thread_id)
#     klid = win32api.GetKeyboardLayout(thread_id)
#     print(win32api.GetKeyboardLayoutName())
#     print(win32api.GetKeyboardState())
#     print(klid)
#     lid = klid & (2 ** 16 - 1)
#     lid_hex = hex(lid)
#     print(lid_hex)
#     if lid_hex == '0x409':
#         print('当前的输入法状态是英文输入模式\n\n')
#     elif lid_hex == '0x804':
#         print('当前的输入法是中文输入模式\n\n')
#     else:
#         print('当前的输入法既不是英文输入也不是中文输入\n\n')
# get_language()

#input_element.send_keys("hello world")
time.sleep(5)
driver.close()